const {getHttpCart} = require("../../models/http")
Page({
  data: {
    carts:""
  },
  onLoad: function (options) {
    getHttpCart({
      success:res=>{
        this.setData({
          carts:res.data
        })
      }
    })
  },
  onChange(event){
    this.handleItem("productCount",event)
  },
  handleItem(attr,event){
    var carts = this.data.carts
    var value = event.detail
    var id = event.currentTarget.dataset.id
    var item = carts.find(item=>item.id==id)
    item[attr] = value
    this.setData({
      carts
    })
  },
  onItemChange(event){
    this.handleItem("isSelected",event)
  },
  onSumChange(event){
    var state = event.detail
    var carts = this.data.carts
    carts.forEach(item=>{
      item.isSelected = state
    })
    this.setData({
      carts
    })
  }
})